#!/bin/bash
# 必要环境安装
# 配置源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum install -y sshpass 

# 读取输入
read -p "输入master的用户名 密码 host 使用空格分隔 如: root root1234 192.168.117.141 > " username passwd host
echo "master的用户名：$username"
echo "master的密码: $passwd"
echo "master的host：$host"
read -p "确认:Please input (Y/N) : " yn

if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then 
    echo "Yes!"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then 
    echo "No!"
    exit 0
else
    echo "Input Error"
    exit 0
fi

echo "注意：输出 [test ssh connect success] 表示配置正确"
sshpass -p $passwd ssh -o StrictHostKeyChecking=no $username@$host "echo 'test ssh connect success'"

# 读取输入
read -p "输入该worker的主机名 - 不要和其他的重名 ！！！！：如: worker1 > " workername
echo "该worker的主机名：$workername"
read -p "确认:Please input (Y/N) : " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then 
    echo "Yes!"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then 
    echo "No!"
    exit 0
else
    echo "Input Error"
    exit 0
fi

echo "Shell Go"



# 必要工具安装
yum update -y && \
yum install -y net-tools telnet nload iftop wget lrzsz && \
yum install -y iotop && \
yum install -y vim sshpass 

# docker 安装
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine && \
yum install -y yum-utils device-mapper-persistent-data lvm2 && \
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && \
yum install -y docker-ce docker-ce-cli && \
yum install -y epel-release && \
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
yum install -y docker-compose

## kubectl 安装

# 1.使用包管理工具安装 必要的k8s管理工具
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
setenforce 0
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

# 2. 设置各种权限相关的配置 
setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 2.1 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 2.3 关闭虚拟交换内存
swapoff -a
# 直接配置中关闭，防止开机重启后kubelet重启失败
sed -i 's/\/dev\/mapper\/centos-swap/#\/dev\/mapper\/centos-swap/' /etc/fstab 
# 2.4 设置docker相关配置
touch /etc/docker/daemon.json
# Set up the Docker daemon
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF
# Create /etc/systemd/system/docker.service.d
sudo mkdir -p /etc/systemd/system/docker.service.d
# Restart Docker
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl enable docker

#2.5 设置bridge
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables

# 3. 初始化control-plane
# 提前下载镜像
docker pull coredns/coredns:1.8.4

docker tag coredns/coredns:1.8.4 registry.aliyuncs.com/google_containers/coredns:v1.8.4

docker rmi coredns/coredns:1.8.4

# 4. worker相关
# 4.1 修改主机名 （主要防止多个节点主机名一致）
hostnamectl set-hostname $workername
# 4.2 下载配置文件 使得worker 也可以使用 kubectl命令
mkdir -p ~/.kube
sshpass -p $passwd scp -o stricthostkeychecking=no  $username@$host:/etc/kubernetes/admin.conf ~/.kube/config
cp ~/.kube/config  /etc/kubernetes/admin.conf
# 4.3 创建加入的token
# master 执行后获取
echo 'join work节点'
# 4.4 join work节点
$(sshpass -p $passwd ssh -o stricthostkeychecking=no $username@$host "kubeadm token create --print-join-command")
